// pages/minePage/bindingCard/index.js
const app = getApp();
import {queryAllRepFixItem, pay} from "../../api/claim";

Page({
    data: {
        totle: '',
        datalist: [],
        totalPrice: 0.00
    },
    onLoad: function (options) {
        var that = this;
        queryAllRepFixItem({"repFixId": options.id}).then(res => {
            let {data, success} = res;
            if (success && data.length > 0) {
                var totalPrice = 0.00;
                data.forEach(item => {
                    totalPrice = (parseFloat(totalPrice) + parseFloat(item.price)).toFixed(2);
                })
                that.setData({
                    datalist: data,
                    totalPrice: totalPrice
                })
            }
        })
    },

    bindInputChange(e) {
        this.setData({
            'totle': e.detail.value
        })
    },

    submitBtn(e) {
        var that = this;
        let totle = this.data.totle*100
        if(totle == '' ||  this.data.totle <=0){
            wx.showToast({
                title: '请输入充值金额',
                icon: 'none',
                duration: 1500
            })
            return
        }
        // if(totle < 10){
        // 	wx.showToast({
        // 		title: '请输入大于10的金额',
        // 		icon: 'none',
        // 		duration: 1500
        // 	})
        // 	return
        // }
        var dot = String(totle).indexOf(".");
        if (dot != -1) {
            var dotCnt = String(totle).substring(dot + 1, totle.length);
            console.log(totle)
            if (dotCnt.length > 2) {
                totle = Number(totle).toFixed(2);
                that.setData({
                    totle: totle
                })
            }
        }
        let data = {
            totalFee: totle,
            openid: app.globalData.openid
        };

        pay(data).then(res => {
            console.log(res, 111);
            // 调起支付
            wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.packageValue,
                signType: 'MD5',
                paySign: res.data.paySign,
                success(res) {
                    wx.showToast({
                        title: '充值成功！',
                        duration: 1500
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1500)
                },
                fail(res) {
                    wx.showToast({
                        title: '充值失败！',
                        duration: 1500
                    })
                }
            })
        })
    },


    onReady: function () {
    },
    onShareAppMessage: function () {
        var shareObj = {
            title: "金和智慧通",
            path: '/pages/index/index',
            imageUrl: '',
            success: function (res) {
                if (res.errMsg == 'shareAppMessage:ok') {
                }
            },
            fail: function () {

            },

        }
        return shareObj;
    }
})
